﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class frmInventory : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if(StoreComboBox.SelectedIndex > Constants.INVALID_ID)
            loadInventoryTable(Convert.ToInt32(StoreComboBox.SelectedValue));
        loadStoreRoomComboBox();
    }

    /// <summary>
    /// Carga el combo box con las bodegas disponibles de la empresa, donde se pueden consultar los artículos
    /// </summary>
    private void loadStoreRoomComboBox()
    {
        StoreComboBox.Items.Clear();
        WebServiceAgent userAgent = (WebServiceAgent)Session[Constants.USER_WEB_SERVICE_CONN];
        ERPSIA.StoreRoom[] storeRooms = userAgent.GetStoreRooms();
        foreach (ERPSIA.StoreRoom store in storeRooms)
            StoreComboBox.Items.Add(new ListItem(store.Code + " - " + store.Name, store.Id.ToString()));
    }

    /// <summary>
    /// Carga la tabla con la información de inventario para una bodega en específico
    /// </summary>
    /// <param name="storeId">Id de la bodega par la que se quiere info de artículos. Este valor se guardo en el combo box</param>
    private void loadInventoryTable(int storeId)
    {
        WebServiceAgent userAgent = (WebServiceAgent)Session[Constants.USER_WEB_SERVICE_CONN];
        ERPSIA.Item[] availableItems = userAgent.GetAvailableItems();
        ERPSIA.StoreRoom store = FinderManager.GetStore(storeId, userAgent.GetStoreRooms());
        foreach (ERPSIA.Item principalItem in availableItems)
        {
            ERPSIA.ItemStoreRoomInfo item = FinderManager.GetStoreInfo(principalItem, store);
            TableRow row = new TableRow();
            TableCell codeCell = new TableCell();
            codeCell.Text = principalItem.CodArticulo;
            TableCell measureCell = new TableCell();
            measureCell.Text = principalItem.MeasureAcronim;
            TableCell descriptionCell = new TableCell();
            descriptionCell.Text = principalItem.Descripción;
            TableCell stockCell = new TableCell();
            stockCell.Text = item.Stock.ToString();
            TableCell availableCell = new TableCell();
            availableCell.Text = (item.Stock + item.Solicitado - item.Comprometido).ToString();
            TableCell commitedCell = new TableCell();
            commitedCell.Text = item.Comprometido.ToString();
            TableCell requestedCell = new TableCell();
            requestedCell.Text = item.Solicitado.ToString();
            TableCell costCell = new TableCell();
            costCell.Text = item.CostoUnitario.ToString();
            TableCell detailCell = new TableCell();
            detailCell.Text = "Ver fotografías";
            row.Cells.Add(codeCell);
            row.Cells.Add(measureCell);
            row.Cells.Add(descriptionCell);
            row.Cells.Add(stockCell);
            row.Cells.Add(availableCell);
            row.Cells.Add(commitedCell);
            row.Cells.Add(requestedCell);
            row.Cells.Add(costCell);
            row.Cells.Add(detailCell);
            ItemTable.Rows.Add(row);
            foreach (TableCell cell in row.Cells)
                cell.HorizontalAlign = HorizontalAlign.Center;
        }
    }

    protected void SearchStoreRoom_Click(object sender, EventArgs e)
    {
    }
}